INS
         NLS 
;
         TTL "DOS 3.3 RWTS"
         ORG $B800
         OBJ $800
;
SLOTZ    EPZ $27
CSUM     EPZ $27
PRIOR    EPZ $27
WTEMP    EPZ $26
TO       EPZ $26
IDX      EPZ $26
COUNT    EPZ $26
LAST     EPZ $26
TRKCNT   EPZ $26
VOLFND   EPZ $2F
TRKFND   EPZ $2E
SECFND   EPZ $2D
SLOTEMP  EPZ $2B
TRKN     EPZ $2A
PTRSDEST EPZ $36
DRIVNO   EPZ $35
BUFADR   EPZ $3E
DEVCTBL  EPZ $3C
MONTIMEH EPZ $47
MONTIMEL EPZ $46
SYNCNT   EPZ $45
TRACK    EPZ $44
VOLUME   EPZ $41
IOBPH    EPZ $49
IOBPL    EPZ $48
DRV0TRK  EQU $478
CURTRK   EQU $478
SEEKCNT  EQU $4F8
DRV1TRK  EQU $4F8
RETRYCNT EQU $578
SLOT     EQU $5F8
SLOTABS  EQU $678
RECALCNT EQU $6F8
CLOSEALL EQU $A316
RSET0    EQU $A75B
TEMP1    EQU $AA63
BYTVAL   EQU $AA70
RUNINTRC EQU $AAB7
SAVFMW   EQU $AE7E
SETERROR EQU $B385
STKSAVE  EQU $B39B
RESTART  EQU $B744
ROM      EQU $C081
PHASEOFF EQU $C080
LC08F    EQU $C08F
LC08E    EQU $C08E
LC08D    EQU $C08D
LC08C    EQU $C08C
LC08B    EQU $C08B
LC08A    EQU $C08A
LC089    EQU $C089
LC088    EQU $C088
BASIC    EQU $E000
SETVID   EQU $FE93
;
;
;
; PRENIBBLIZE ROUTINE
;
; CONVERTS 256 BYTES POINTED AT BY
; BUFADR TO 342 6-BIT NIBBLES
; OF THE FORM 00XXXXXX
;
PRENIB16 LDX #0
         LDY #2
;
; Get next user byte
;
PRENIB1  DEY 
         LDA (BUFADR),Y
;
; Shift H.O. two bytes into
; NBUF2.
;
         LSR 
         ROL NBUF2,X
         LSR 
         ROL NBUF2,X
;
; Put L.O. six bits (shifted left)
; into NBUF1.
;
         STA NBUF1,Y
         INX 
         CPX #$56
         BCC PRENIB1
;
;
; Done yet?
;
         LDX #0
         TYA 
         BNE PRENIB1
;
;
; strip H.O. two bits of NBUF2. 
;
         LDX #$55
PRENIB2  LDA NBUF2,X
         AND #$3F
         STA NBUF2,X
         DEX 
         BPL PRENIB2
         RTS 
;
;
; Write subroutine
;
; Writes prenibbilized data in
; NBUF1 and NBUF2 to disk.
;
; note: this stuff is all time
; critical.
;
; watch page boundries, don't
; remove NOP's, etc.
;
;
;
WRITE16  SEC                 ;anticipate write protect
         STX SLOTZ
         STX SLOTABS
         LDA LC08D,X
         LDA LC08E,X         ;sense write protect
         BMI WEXIT
         LDA NBUF2
         STA WTEMP
         LDA #$FF            ;sync byte
         STA LC08F,X         ;write 1st nibble
         ORA LC08C,X
         PHA 
         PLA 
         NOP 
         LDY #4
;
;
; Write the sync byte
;
WSYNC    PHA 
         PLA 
         JSR WNIBL7
         DEY 
         BNE WSYNC
         LDA #$D5            ;first data mark
         JSR WNIBL9
         LDA #$AA            ;2nd data mark
         JSR WNIBL9
         LDA #$AD            ;3rd data mark
         JSR WNIBL9
         TYA                 ;clear checksum
         LDY #$56            ;NBUF2 index
         BNE >1              ;always taken
;
;
;
WDATA0   LDA NBUF2,Y         ;get prior 6-bit nibble
^1       EOR NBUF2-1,Y       ;and XOR with current nibble
         TAX 
         LDA NIBL,X
         LDX SLOTZ
         STA LC08D,X         ;write nibble
         LDA LC08C,X
         DEY                 ;next nibble
         BNE WDATA0
;
;
;
; now handle NBUF1.
;
;
; get prior nibble
;
         LDA WTEMP
         NOP 
;
; loop to write out data in NBUF1
;
WDATA2   EOR NBUF1,Y
         TAX 
         LDA NIBL,X
         LDX SLOTABS
         STA LC08D,X         ;write nibble
         LDA LC08C,X
         LDA NBUF1,Y
         INY 
         BNE WDATA2
         TAX 
         LDA NIBL,X
;
; write checksum
;
         LDX SLOTZ
         JSR WNIBL
;
; write epilog to data
;
         LDA #$DE            ;DM4, bit slip mark
         JSR WNIBL9
         LDA #$AA            ;DM5, bsm
         JSR WNIBL9
         LDA #$EB            ;DM6, bsm
         JSR WNIBL9
;
; All done, close up the shop!
;
         LDA #$FF
         JSR WNIBL9
;
; turn off the write mode
;
         LDA LC08E,X
;
; and back to the read mode
;
WEXIT    LDA LC08C,X
         RTS 
;
;
;
; WNIBL9 9 cycles, then write
;
WNIBL9   CLC 
;
; WNIBL7 7 cycles, then write
;
WNIBL7   PHA 
         PLA 
;
;
; write nibble to disk
;
WNIBL    STA LC08D,X
         ORA LC08C,X
         RTS 
;
;
; Post nibblize routine.
;
; converts 342 nibbles of the form
;
;     00xxxxxx
;
; to eight bit bytes.
;
;  The nibbles are stored in NBUF1
;  and NBUF2, the 8-bit bytes will
;  be stored at (BUFADR).
;
;
POSTNB16 LDY #0
POST1    LDX #$56
POST2    DEX 
         BMI POST1
;
; get byte and shift in L.O. two
; bits from NBUF2
;
         LDA NBUF1,Y
         LSR NBUF2,X
         ROL 
         LSR NBUF2,X
         ROL 
;
; store in user buffer
;
         STA (BUFADR),Y
         INY 
         CPY TO
         BNE POST2
         RTS 
;
;
; READ routine, reads a sector
; from the disk and stores the
; data in NBUF1 and NBUF2.
;
READ16   LDY #$20
;
; get sync bytes
;
RSYNC    DEY 
         BEQ RDERR
;
; wait until a byte is recieved
; from the disk drive.
;
^1       LDA LC08C,X
         BPL <1
;
; byte recieved, check for DM1
;
RSYNC1   EOR #$D5
         BNE RSYNC
         NOP 
;
; get next byte and check for
; DM2
;
^2       LDA LC08C,X
         BPL <2
         CMP #$AA
         BNE RSYNC1
         LDY #$56
;
; Check for DM3
;
^3       LDA LC08C,X
         BPL <3
         CMP #$AD
         BNE RSYNC1
;
; Read the data from the sector
;
         LDA #0              ;init checksum
;
; Read stuff into NBUF2
;
RDATA1   DEY 
         STY IDX
^4       LDY LC08C,X
         BPL <4
         EOR MSWAIT,Y
         LDY IDX
         STA NBUF2,Y
         BNE RDATA1
;
; Read stuff into NBUF1
;
RDATA2   STY IDX
^5       LDY LC08C,X
         BPL <5
         EOR MSWAIT,Y
         LDY IDX
         STA NBUF1,Y
         INY 
         BNE RDATA2
;
; get and check the checksum byte
;
^6       LDY LC08C,X
         BPL <6
         CMP MSWAIT,Y
         BNE RDERR
;
; check for DM4
;
^7       LDA LC08C,X
         BPL <7
         CMP #$DE
         BNE RDERR
         NOP 
;
; check for DM5
;
^8       LDA LC08C,X
         BPL <8
         CMP #$AA
         BEQ RDADRX
RDERR    SEC 
         RTS 
;
;
;  Read address field.
;
; Reads starting address marks
; ($D5, $AA, $96), address info
; (volume/track/sector/checksum),
; and closing address marks
; ($DE, $AA)
;
RDADR16  LDY #$FC
         STY COUNT
RDASYN   INY 
         BNE >0
         INC COUNT
         BEQ RDERR
;
; Read first address mark ($D5)
;
^0       LDA LC08C,X
         BPL <0
RDASYN1  CMP #$D5
         BNE RDASYN
         NOP 
;
; Read next address mark
;
^1       LDA LC08C,X
         BPL <1
         CMP #$AA
         BNE RDASYN1
         LDY #3
;
; Read last address mark
;
^2       LDA LC08C,X
         BPL <2
         CMP #$96
         BNE RDASYN1
;
; init checksum and read the
; address data field (four bytes)
;
         LDA #0
RDAFLD   STA CSUM
;
; Read 'odd' bit nibble
;
^3       LDA LC08C,X
         BPL <3
         ROL 
         STA LAST
;
; Read even bit nibble.
;
^4       LDA LC08C,X
         BPL <4
;
; Merge the two.
;
         AND LAST
;
; store the data byte, update
; checksum, and repeat until
; entire address field is read.
;
         STA SLOTEMP+1,Y
         EOR CSUM
         DEY 
         BPL RDAFLD
;
; Checksum (in acc) must be 0.
;
         TAY 
         BNE RDERR
;
; Read first epilogue byte ($DE)
;
^5       LDA LC08C,X
         BPL <5
         CMP #$DE
         BNE RDERR
         NOP 
;
; Read second epilogue byte ($AA)
;
^6       LDA LC08C,X
         BPL <6
         CMP #$AA
         BNE RDERR
RDADRX   CLC 
         RTS 
;
;
; SEEKABS routine, moves the
; disk head over the desired
; track.
;
SEEKABS  STX SLOTEMP
         STA TRKN
         CMP DRV0TRK
         BEQ RTS0
         LDA #0
         STA TRKCNT
MOVEHEAD LDA DRV0TRK
         STA PRIOR
         SEC 
         SBC TRKN
         BEQ ISTHERE
         BCS >0
         EOR #$FF
         INC DRV0TRK
         BCC >1
^0       ADC #$FE
         DEC DRV0TRK
^1       CMP TRKCNT
         BCC >2
         LDA TRKCNT
^2       CMP #$C
         BCS >3
         TAY 
^3       SEC 
         JSR CHKPOS
         LDA ONTBL,Y
         JSR MSWAIT
         LDA PRIOR
         CLC 
         JSR CHKPOS2
         LDA OFFTBL,Y
         JSR MSWAIT
         INC TRKCNT
         BNE MOVEHEAD
ISTHERE  JSR MSWAIT
         CLC 
CHKPOS   LDA DRV0TRK
CHKPOS2  AND #3
         ROL 
         ORA SLOTEMP
         TAX 
         LDA PHASEOFF,X
         LDX SLOTEMP
RTS0     RTS 
         TAX 
         LDY #$A0
;
;
; Head move delay subroutine
; delays ACC*100 usec
;
;
MSWAIT   LDX #$11
^2       DEX                 ;delay 86 usec
         BNE <2
         INC MONTIMEL
         BNE >3
         INC MONTIMEH
^3       SEC 
         SBC #1
         BNE MSWAIT
         RTS 
;
;
; PHASEON/PHASEOFF tables
;
ONTBL    HEX 0130
         HEX 2824201E1D1C1C1C1C1C
OFFTBL   HEX 702C26221F1E1D1C1C1C1C1C
;
;
; Write translate tables
;
NIBL     HEX 96979A9B9D9E9FA6A7ABACADAEAFB2B3
         HEX B4B5B6B7B9BABBBCBDBEBFCBCDCECFD3
         HEX D6D7D9DADBDCDDDEDFE5E6E7E9EAEBEC
         HEX EDEEEFF2F3F4F5F6F7F9FAFBFCFDFEFF
;
; ALIGN REMAINDER OF TABLE
;
         DFS 45
;
;
         HEX 0001989902039C040506A0A1A2A3A4A5
         HEX 0708A8A9AA090A0B0C0DB0B10E0F1011
         HEX 1213B81415161718191AC0C1C2C3C4C5
         HEX C6C7C8C9CA1BCC1C1D1ED0D1D21FD4D5
         HEX 2021D822232425262728E0E1E2E3E429
         HEX 2A2BE82C2D2E2F303132F0F133343536
         HEX 3738F8393A3B3C3D3E3F
;
;
;
; nibble buffers, must be in this
; order!
;
NBUF1    DFS $100
NBUF2    DFS $56
;
;
;
; Write an address field routine
;
;
WRADR16  SEC                 ;assume W/P error
         LDA LC08D,X
         LDA LC08E,X
         BMI WPERROR
;
; output sync byte
;
         LDA #$FF
         STA LC08F,X
         CMP LC08C,X
         PHA 
         PLA 
FRMSYNC  JSR WAIT12
         JSR WAIT12
         STA LC08D,X
         CMP LC08C,X
         NOP 
         DEY 
         BNE FRMSYNC
;
; output data marks
;
         LDA #$D5
         JSR WBYTE9
         LDA #$AA
         JSR WBYTE9
         LDA #$96
         JSR WBYTE9
;
; output volume track sector
; and checksum
;
         LDA VOLUME
         JSR WBYTE
         LDA TRACK
         JSR WBYTE
         LDA BUFADR+1
         JSR WBYTE
         LDA VOLUME
         EOR TRACK
         EOR BUFADR+1
         PHA 
         LSR 
         ORA BUFADR
         STA LC08D,X
         LDA LC08C,X
         PLA 
         ORA #$AA
         JSR WBYTE11
;
; output data marks 4,5, and 6
;
         LDA #$DE
         JSR WBYTE9
         LDA #$AA
         JSR WBYTE9
         LDA #$EB
         JSR WBYTE9
         CLC 
WPERROR  LDA LC08E,X
         LDA LC08C,X
WAIT12   RTS 
;
;
; Write a byte as two four bit
; nibbles to the disk.
;
WBYTE    PHA 
         LSR 
         ORA BUFADR
;
; write odd bits
;
         STA LC08D,X
         CMP LC08C,X
         PLA 
         NOP 
         NOP 
         NOP 
         ORA #$AA
WBYTE11  NOP 
WBYTE9   NOP 
         PHA 
         PLA 
;
; write even bits
;
         STA LC08D,X
         CMP LC08C,X
         RTS 
;
;
; FORCE RWTS TO PAGE BOUNDRY
;
         DFS 33
;
;
;
; RWTS entry point
;
;
RWTS:
;
; upon entry, A & Y point at IOB
;
         STY IOBPL
         STA IOBPH
;
; set up for one recal and 4 seeks
;
         LDY #2
         STY RECALCNT
         LDY #4
         STY SEEKCNT
;
; get slot #
;
         LDY #1
         LDA (IOBPL),Y
         TAX 
;
;
; see if slot # was changed
;
         LDY #$F
         CMP (IOBPL),Y
         BEQ SAMESLOT
;
; if so, turn off old drive
;
         TXA 
         PHA 
         LDA (IOBPL),Y
         TAX 
         PLA 
         PHA 
         STA (IOBPL),Y
         LDA LC08E,X
;
; delay until data is constant
;
STILLON  LDY #8
         LDA LC08C,X
NOTSURE  CMP LC08C,X
         BNE STILLON
         DEY 
         BNE NOTSURE
         PLA 
         TAX 
;
;
; start the motor up
;
SAMESLOT LDA LC08E,X
         LDA LC08C,X
         LDY #8
NOTSURE2 LDA LC08C,X
         PHA 
         PLA 
         PHA 
         PLA 
         STX SLOT
         CMP LC08C,X
         BNE >3
         DEY 
         BNE NOTSURE2
^3       PHP 
         LDA LC089,X
;
; move necessary pointers to
; page zero
;
         LDY #6
PTRMOVE  LDA (IOBPL),Y
         STA PTRSDEST,Y
         INY 
         CPY #$A
         BNE PTRMOVE
;
; get other parameters
;
         LDY #3
         LDA (DEVCTBL),Y
         STA MONTIMEH
         LDY #2
         LDA (IOBPL),Y
         LDY #$10
         CMP (IOBPL),Y
         BEQ >4
         STA (IOBPL),Y
         PLP 
         LDY #0
         PHP 
^4       ROR 
         BCC >5
         LDA LC08A,X
         BCS DRVSEL
^5       LDA LC08B,X
;
; save which drive is being used
;
DRVSEL   ROR DRIVNO
         PLP 
         PHP 
         BNE NOWAIT
         LDY #7
;
; wait 100 usec for old drive's
; timing capacitor to discharge
;
STEPWAIT JSR MSWAIT
         DEY 
         BNE STEPWAIT
         LDX SLOT
NOWAIT   LDY #4
         LDA (IOBPL),Y
         JSR MYSEEK
         PLP 
         BNE TRYTRK
         LDY MONTIMEH
         BPL TRYTRK
;
; wait for motor to come up to
; speed.
;
MOTOROFF LDY #$12
^6       DEY 
         BNE <6
         INC MONTIMEL
         BNE MOTOROFF
         INC MONTIMEH
         BNE MOTOROFF
;
; disk is now up to speed.
; if not format operation,
; position the head over the
; proper track
;
TRYTRK   LDY #$C
         LDA (IOBPL),Y
         BEQ GALLDONE
         CMP #4
         BEQ FORMDSK
         ROR 
         PHP                 ;save R/W status
         BCS TRYTRK2
;
; if a read operation, must
; prenibblize first.
;
         JSR PRENIB16
;
;
; set up for a maximum of 48
; retries.
;
TRYTRK2  LDY #$30
         STY RETRYCNT
TRYADR   LDX SLOT
         JSR RDADR16
         BCC RDRIGHT
TRYADR2  DEC RETRYCNT
         BPL TRYADR
;
; recalibrate disk head
;
RECAL    LDA CURTRK
         PHA 
         LDA #$60
         JSR SETTRK
         DEC RECALCNT
         BEQ DRVERROR
         LDA #4
         STA SEEKCNT
         LDA #0
         JSR MYSEEK
         PLA 
RESEEK   JSR MYSEEK
         JMP TRYTRK2
;
;
; We have just read an address
; field, now check for desired
; track, sector, and volume
;
RDRIGHT  LDY TRKFND
         CPY CURTRK
         BEQ RTTRK
;
; Save dest track value
;
         LDA CURTRK
         PHA 
         TYA 
         JSR SETTRK
         PLA 
         DEC SEEKCNT
         BNE RESEEK
         BEQ RECAL
;
; Bad drive error.
;
DRVERROR PLA 
         LDA #$40
;
;
JMPTO1   PLP 
         JMP HNDLERR
GALLDONE BEQ ALLDONE
FORMDSK  JMP DSKFORM
;
;
; Drive is on the right track,
; now check for a vol mismatch
;
RTTRK    LDY #3
         LDA (IOBPL),Y       ;get desired volume
         PHA 
;
; Save volume actually found in
; RWTS IOB.
;
         LDA VOLFND
         LDY #$E
         STA (IOBPL),Y
;
; If volume specified was zero,
; no error.
;
         PLA 
         BEQ CRCTVOL
;
; Otherwise, check for a volume
; mismatch error.
;
         CMP VOLFND
         BEQ CRCTVOL
         LDA #$20
         BNE JMPTO1
;
; now check for the correct sector
;
CRCTVOL  LDY #5
         LDA (IOBPL),Y       ;get the sector #
;
; Convert to a "soft" sector
; number by applying the software
; interleave.
;
         TAY 
         LDA INTRLEAV,Y
;
; Are we at that sector yet?
;
         CMP SECFND
         BNE TRYADR2
;
; If so, see if we are doing a
; read or a write.
;
         PLP 
         BCC WRIT
;
; Reading, so read in the 256 
; bytes of data that follow.
;
         JSR READ16
         PHP 
         BCS TRYADR2
         PLP 
         LDX #0
         STX TO
;
; Convert the nibbles to bytes.
;
         JSR POSTNB16
         LDX SLOT
ALLDONE  CLC 
         BYT $24             ;BIT L38  
HNDLERR  SEC                 ;OPCODE SKIPPED BY BIT L38
         LDY #$D
         STA (IOBPL),Y
         LDA LC088,X
         RTS 
;
; Performing a write, write the
; data (already nibblized) to
; the following data sector.
;
WRIT     JSR WRITE16
         BCC ALLDONE
         LDA #$10
         BCS HNDLERR
;
;
; MYSEEK is the seek routine,
; it seeks track 'N' in slot
; X/16
;
; If DRIVNO is negative - drive 0
; If DRIVNO is positive - drive 1
;
MYSEEK   PHA 
         LDY #1
         LDA (DEVCTBL),Y
         ROR 
         PLA 
         BCC SEEK1
         ASL 
         JSR SEEK1
         LSR CURTRK
         RTS 
SEEK1    STA TRKN
         JSR XTOY
         LDA DRV0TRK,Y
         BIT DRIVNO
         BMI WASDO
         LDA DRV1TRK,Y
WASDO    STA DRV0TRK
         LDA TRKN
         BIT DRIVNO
         BMI ISDRV0
         STA DRV1TRK,Y
         BPL GOSEEK
ISDRV0   STA DRV0TRK,Y
GOSEEK   JMP SEEKABS
XTOY     TXA 
         LSR 
         LSR 
         LSR 
         LSR 
         TAY 
         RTS 
;
;
; This routine sets the slot
; dependant track location
;
SETTRK   PHA 
         LDY #2
         LDA (IOBPL),Y
         ROR 
         ROR DRIVNO
         JSR XTOY
         PLA 
         ASL 
         BIT DRIVNO
         BMI ONDRV0
         STA DRV1TRK,Y
         BPL RTS3
ONDRV0   STA DRV0TRK,Y
RTS3     RTS 
;
;
; This is the disk formatter
; routine.
;
DSKFORM  LDY #3
         LDA (IOBPL),Y
         STA VOLUME
;
; Save timing constant in zero
; page (for time critical section)
;
         LDA #$AA
         STA BUFADR
         LDY #$56
;
; Set up to start a track zero.
;
         LDA #0
         STA TRACK
;
;
; zero secondary buffer.
;
^9       STA NBUF2-1,Y
         DEY 
         BNE <9
;
; zero primary buffer.
;
^0       STA NBUF1,Y
         DEY 
         BNE <0
;
; Pretend we are at track 40
; (acc=2*maxtracks)
;
         LDA #$50
         JSR SETTRK
;
; Start with 40 bytes of self
; sync bytes.
;
         LDA #$28
         STA SYNCNT
;
; Got to the track and format it.
;
NXTTRK   LDA TRACK
         JSR MYSEEK
         JSR DISKF2
;
; Init in case of DISK I/O error.
;
         LDA #8
         BCS HNDERR
         LDA #$30
         STA RETRYCNT
;
; Verify the track just formatted.
;
NOGOOD   SEC 
         DEC RETRYCNT
         BEQ HNDERR
;
; Read the address field.
;
         JSR RDADR16
         BCS NOGOOD          ;something wrong?
;
         LDA SECFND
         BNE NOGOOD
;
; Read the data and see if it's
; ok.
;
         JSR READ16
         BCS NOGOOD
;
; A-OK, move on to the next track.
;
         INC TRACK
;
; At the last track yet?
;
         LDA TRACK
         CMP #$23
         BCC NXTTRK
;
; Force error flag (carry) off.
;
         CLC 
         BCC DONEDSK
;
;
; Store error code in IOB.
;
HNDERR   LDY #$D
         STA (IOBPL),Y
         SEC 
;
; Turn the disk off.
;
DONEDSK  LDA LC088,X
         RTS 
;
; Format the current track.
;
DISKF2   LDA #0              ;init sector #
         STA BUFADR+1
         LDY #$80            ;begin track with 128 sync bytes
;
         BNE >3
;
;
TRKFRM   LDY SYNCNT
^3       JSR WRADR16         ;write an address field
         BCS DELAY12
;
; Write a data field.
;
         JSR WRITE16
         BCS DELAY12
;
; Increment the sector number
; and see if it is 16 yet.
;
         INC BUFADR+1
         LDA BUFADR+1
         CMP #$10
         BCC TRKFRM
;
; Reset sector # to 15
;
         LDY #$F
         STY BUFADR+1
;
; mark the current track as 
; formatted.
;
         LDA #$30
         STA RETRYCNT
INITMAP  STA SECMAP,Y
         DEY 
         BPL INITMAP
         LDY SYNCNT
DELAY    JSR DELAY12
         JSR DELAY12
         JSR DELAY12
         PHA 
         PLA 
         NOP 
         DEY 
         BNE DELAY
         JSR RDADR16
         BCS DOAGAIN
         LDA SECFND
         BEQ ITSGOOD
         LDA #$10
         CMP SYNCNT
         LDA SYNCNT
         SBC #1
         STA SYNCNT
         CMP #5
         BCS DOAGAIN
         SEC 
         RTS 
NXTSEC   JSR RDADR16
         BCS NXTTRY
ITSGOOD  JSR READ16
         BCC MARKMAP
NXTTRY   DEC RETRYCNT
         BNE NXTSEC
DOAGAIN  JSR RDADR16
         BCS ERRCNT
         LDA SECFND
         CMP #$F
         BNE ERRCNT
         JSR READ16
         BCC DISKF2
ERRCNT   DEC RETRYCNT
         BNE DOAGAIN
         SEC 
DELAY12  RTS 
MARKMAP  LDY SECFND
         LDA SECMAP,Y
         BMI NXTTRY
         LDA #$FF
         STA SECMAP,Y
         DEC BUFADR+1
         BPL NXTSEC
         LDA TRACK
         BNE TRKDONE
         LDA SYNCNT
         CMP #$10
         BCC DELAY12
         DEC SYNCNT
         DEC SYNCNT
TRKDONE  CLC 
         RTS 
;
;
; SECMAP- used to mark initialized
; sectors.
;
SECMAP   HEX FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
;
;
; Interleave remapping table
;
INTRLEAV HEX 000D0B09070503010E0C0A080604020F
;
;
;
; DOS 3.3 patches.  WARNING! These
; are addressed by object code
; and should not be moved without
; careful thought.
;
         JSR SETVID
         LDA ROM
         LDA ROM
         LDA #0
         STA BASIC
         JMP RESTART
;
; UNUSED
;
         DFS 3
;
;
         STA TEMP1
         STA BYTVAL
         STA BYTVAL+1
         RTS 
         JSR RSET0
         STY RUNINTRC
         RTS 
         JSR SAVFMW
         LDX STKSAVE
         TXS 
         JSR CLOSEALL
         TSX 
         STX STKSAVE
         LDA #9
         JMP SETERROR
         END 




